package com.lightbox.android.photoprocessing;


import com.jihox.abookpro.R;
import com.lightbox.android.photoprocessing.toning.ToneLayer;
import com.lightbox.android.photoprocessing.utils.BitmapUtils;
import com.lightbox.android.photoprocessing.utils.FileUtils;
import com.lightbox.android.photoprocessing.utils.MediaUtils;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class PhotoToningActivity extends Activity implements OnSeekBarChangeListener {
	private ToneLayer mToneLayer;  
	private ImageView mImageView;  
	private Bitmap mBitmap;
	private SeekBar mSaturationSK;
	private SeekBar mLightnessSK;
	private SeekBar mContrastSK;
	private SeekBar mCurrentSK;
	private TextView mSaturation_bt;
	private TextView mLightness_bt;
	private TextView mContrast_bt;
	private TextView save_toning;
	private TextView mCurrentTextView;
	private TextView back_to_main;
	private static Bitmap resultBit;
	private ProgressDialog mProgressDialog = null;
	
	public static int SELECTED_COLOR = Color.YELLOW;
	public static int UNSELECTED_COLOR = Color.WHITE;
	
	private String mCurrentPhotoPath;
	/** 
     * 饱和度标识 
     */  
    public static final int FLAG_SATURATION = 0x0;  
      
    /** 
     * 亮度标识 
     */  
    public static final int FLAG_LUM = 0x1;  
      
    /** 
     * 色相标识 
     */  
    public static final int FLAG_HUE = 0x2;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);	
		setContentView(R.layout.activity_phtot_toning);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		init();  
	}


	      
	private void init(){
		
		mSaturationSK = (SeekBar) findViewById(R.id.saturation_seekbar);
		mLightnessSK = (SeekBar) findViewById(R.id.lightness_seekbar);
		mContrastSK = (SeekBar) findViewById(R.id.contrast_seekbar);
		mImageView = (ImageView) findViewById(R.id.img_view);  
		mSaturation_bt = (TextView) findViewById(R.id.saturation_bt);
		mLightness_bt = (TextView) findViewById(R.id.lightness_bt);
		mContrast_bt = (TextView) findViewById(R.id.contrast_bt);
		save_toning = (TextView) findViewById(R.id.save_toning);
		back_to_main = (TextView) findViewById(R.id.back_bt);
		
		mSaturationSK.setTag(FLAG_SATURATION);
		mSaturationSK.setOnSeekBarChangeListener(this);
		mLightnessSK.setTag(FLAG_LUM);
		mLightnessSK.setOnSeekBarChangeListener(this);
		mContrastSK.setTag(FLAG_HUE);
		mContrastSK.setOnSeekBarChangeListener(this);
		
		save_toning.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

					showSavingProgressDialog();
					String nsavepath = FileUtils.savePhoto(resultBit, mCurrentPhotoPath,true);
					Intent mIntent=new Intent();
					mIntent.putExtra("toningImagePath", nsavepath);
					setResult(RESULT_OK, mIntent);
					hideProgressDialog();
					finish();

			}
		});
		back_to_main.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		mSaturation_bt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mCurrentTextView.setTextColor(UNSELECTED_COLOR);
				mCurrentSK.setVisibility(View.GONE);
				mCurrentTextView = mSaturation_bt;
				mCurrentSK = mSaturationSK;
				mCurrentTextView.setTextColor(SELECTED_COLOR);
				mCurrentSK.setVisibility(View.VISIBLE);
				
			}
		});
		mLightness_bt.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						mCurrentTextView.setTextColor(UNSELECTED_COLOR);
						mCurrentSK.setVisibility(View.GONE);
						mCurrentTextView = mLightness_bt;
						mCurrentSK = mLightnessSK;
						mCurrentTextView.setTextColor(SELECTED_COLOR);
						mCurrentSK.setVisibility(View.VISIBLE);
					}
				});
		mContrast_bt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mCurrentTextView.setTextColor(UNSELECTED_COLOR);
				mCurrentSK.setVisibility(View.GONE);
				mCurrentTextView = mContrast_bt;
				mCurrentSK = mContrastSK;
				mCurrentTextView.setTextColor(SELECTED_COLOR);
				mCurrentSK.setVisibility(View.VISIBLE);
			}
		});
		
		
        mToneLayer = new ToneLayer();  
        mCurrentPhotoPath=getIntent().getStringExtra("mOriginalPhotoPath");
        loadPhoto(mCurrentPhotoPath);
//        mBitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);
        resultBit = mBitmap;
        mImageView.setImageBitmap(mBitmap);  


        
        mCurrentTextView = mSaturation_bt;
        mCurrentSK = mSaturationSK;
        mCurrentTextView.performClick();
    }  
  
    @Override  
    public void onProgressChanged(SeekBar seekBar, int progress,  
            boolean fromUser) {  
        int flag = (Integer) seekBar.getTag();  
        switch (flag)  
        {  
        case ToneLayer.FLAG_SATURATION:  
            mToneLayer.setSaturation(progress);  
            break;  
        case ToneLayer.FLAG_LUM:  
            mToneLayer.setLum(progress);  
            break;  
        case ToneLayer.FLAG_HUE:  
            mToneLayer.setHue(progress);  
            break;  
        }  
        resultBit  = mToneLayer.handleImage(mBitmap, flag);
        mImageView.setImageBitmap(resultBit);  
    }  
  
    @Override  
    public void onStartTrackingTouch(SeekBar seekBar) {  
          
    }  
  
    @Override  
    public void onStopTrackingTouch(SeekBar seekBar) {  
          
    }  

    private void loadPhoto(String path) {
		DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
		
		if (mBitmap != null) {
			mBitmap.recycle();
		}
		
		mBitmap = BitmapUtils.getSampledBitmap(path, displayMetrics.widthPixels, displayMetrics.heightPixels,true);
		
		if (mBitmap != null && !mBitmap.isMutable()) {
			mBitmap = PhotoProcessing.makeBitmapMutable(mBitmap);
		}
		
		int angle = MediaUtils.getExifOrientation(path);
		mBitmap = PhotoProcessing.rotate(mBitmap, angle);
		

	}

	private void showSavingProgressDialog() {
		String message = "正在处理...";
		mProgressDialog = ProgressDialog.show(this, "", message);
	}

	private void hideProgressDialog() {
		if (mProgressDialog != null && mProgressDialog.isShowing()) {
			mProgressDialog.dismiss();
		}
	}

}
